GBML manual / Lists

list_keep

Keeps only the items of target that the script test says yes to, and takes out the ones it says no to - in the list itself, with no second list made. The items that stay keep their order. test is a script that takes one argument (the item) and gives back true or false: the same kind of script list_filter uses, where true means keep. Do not add to or take from target inside that script.

Syntax

list_keep(target, test)

Arguments

NameType
targetlist
testscript

Returns

Nothing.

Description

Takes out of target, a list, every item that a script says no to, and keeps the ones it says yes to. The script is asked about each item in turn. It answers true to keep the item and false to take it out. The items that stay keep the order they had. It changes target itself and makes no second list.

list_keep(scores, is_high);

If scores holds 30, 80, 55 and 10 and is_high says yes to 50 or more, scores holds 80 and 55 afterwards, in that order, and list_length says 2.

listkeep or listfilter

list_filter asks the same question with the same kind of script, but it leaves target alone and makes a new list of the items that passed. list_keep does the job inside the list you already have.

list_filterlist_keep
what it does to targetnothing: it is not changedthe items the script said no to are taken out
memorya whole second list, as big as target can ever benone
where you can write itonly as the whole right-hand side of the line that makes a variableon a line of its own, anywhere, as often as you like
works ona list, an array or a grida list only

Use list_filter when you need the original as well. Use list_keep when you do not, and above all in a game that filters over and over: bullets that are still flying, enemies that are not beaten yet. It costs no memory, so doing it every frame is fine.

The script

The second thing you give is a script that answers true or false for one item, the same kind of script list_filter takes, and it has to follow the same rules: it takes exactly one parameter, which is where each item arrives, and the items have to fit that parameter's type. The script is_high above can be handed to either function.

// scripts/is_high.gbml
function is_high(score: int): bool {
    return score >= 50;
}

Do not change the list from inside the script

While list_keep is working through the list, the script must not add items to that list or take items out of it. GBML cannot see it happening. If it does happen, nothing breaks: nothing outside the list is ever read or written, and the pass always ends. But which items are left afterwards is not promised, so do not rely on it. Looking at the list from inside the script does no harm, but do not count on what you find there: the items that stay are moved down as the pass goes, so the list is part way between the old one and the new one.

A list only

An array and a grid have a fixed number of places, so nothing can be taken out of them, and list_keep on one is refused when the project is checked. A stack, a queue, a set and a map have no "each item in order" to ask about, so they have no list_keep either. To get the items an array or a grid passes, use array_filter or grid_filter, which make a list.

What happens in the awkward cases

You do thisWhat happens
keep from a list with no itemsnothing happens, and the script is never called
the script says no to every itemthe list is empty afterwards
the script says yes to every itemthe list is left as it is
a list of text, structs or referencesworks. Nothing is compared, the script decides
give a script that is not a script, has the wrong number of parameters, the wrong kind of parameter, or does not answer true or falserefused when checked, and the message says which
give it an array, a grid, a stack or a queuerefused when checked
use it where a value is neededrefused when checked. It gives nothing back

What it costs

One call of your script for each item the list holds, and at most one move of an item for each item. Nothing is set aside, whatever the size of the list. Taking items out with list_remove in a loop costs more, because every removal moves all the items after it. No timing was measured.

Example

/// @type {list<int>}
var scores = [30, 80, 55, 10];
list_keep(scores, is_high);              // 80, 55
var how_many = list_length(scores);      // 2
var best = scores[0];                    // 80

On the GBA

One pass over the list_length(target) items the list holds, from the first to the last, with a second place behind it: each item is copied once, the script is asked about the copy, and an item it says yes to is moved down to the next free place at the front (it does not move at all while nothing before it has been taken out). When the pass is over, the places after the last kept item are taken off the end of the list. O(n): one call of your script per item and at most one move per item. Unlike list_filter it sets no memory aside - no second list, whatever the capacity - so it is the one to use in a game that filters every frame. The kept items keep their order. The script has to give back true or false and take exactly one argument whose type accepts the items. Do not add to or take from the list inside the script: GBML cannot see that it happens. If it does, nothing outside the list is read or written (the length is read again at every step, the item is copied before the script is asked about it, and a place is checked against the length before it is written), but which items are left is not promised. It works on a list only: an array and a grid have a fixed number of places, so nothing can be taken out of them, and a stack, queue, set or map has no "each item in order". No timing was measured.

See also: list_filterlist_removelist_clearlist_foreachlist_lengthlist_copyListsScriptsWriting a scriptData containers